/**
 * class name: idata,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://jsease.googlecode.com/svn/trunk/core.js, inav.js,
 * update: http://jsease.googlecode.com/svn/trunk/idata.js
 * -------
 */
function idata() {
    var A = arguments,
        _ = this;
    _ = ext( _, ext(
        {
        id: 'idata',
        elem: me.body,
        cls: 'idata',
        tool: {cls:'', menu:''},
        title:[{col:'',text:'',cls:''}],
        data: {
            data1:{
                'col0':'',
                'col1':'',
                'col9':''
                }
            },
        fn:''
        }, A[0] ) );

    _.uitype = 'idata';

    _.elem = $(_.elem) || me.body;
}
idata.prototype = {
    checkall : function (t) {
        for ( var i=0,L=this.idata.length; i<L; i++) {
            this.idata[i].checked = t?true:false;
        }
    return this;
    },

    seTitle : function () {
        /* title */
        var _ = this, titl = elm('div').setClass('item titl').appendTo( this.ititl ),
            chkall = elm('input').attr('type','checkbox').appendTo(elm('div').setClass('icol chk').appendTo(titl));
        chkall.onclick = function(){
            _.checkall(this.checked);
        };

        for ( var i=0, L=this.title.length; i<L; i++) {
            var tit = elm('div').setClass('icol'+ (p(this.title[i],'cls')? ' '+this.title[i].cls : '')).html(this.title[i].text||'--').appendTo(titl);
        }

    return this;
    },

    add : function (od, del) {
        if ( !od || 'object'!=typeof(od) ) {
            return this;
        } else {
            ext(this.data, od);
        }

        /* items */
        for ( var key in od ) {
            if ( !!del ) {
                $(key) && $(key).empty().remove();
                delete this.data[key];
                continue;
            }

            var item = $(key) ? $(key).empty() : elm('div').attr('id', key).setClass('item').appendTo( this.idata );
            if ( this.idata.childNodes.length%2==1 ) {
                item.addClass('odd');
            }

            /* cells */
            var dchk = elm('div').setClass('icol chk').appendTo(item);
            var chk = elm('input').attr('type','checkbox').attr('name','guid').attr('value', key).appendTo(dchk);
            for ( var j=0,L=this.title.length; j<L; j++) {
                if ( !p(this.title[j],'col') ) {
                    continue;
                }
                var col = this.title[j].col,
                    cls = p(this.title[j],'cls') ? ' '+this.title[j].cls : '';
                if ( hop(od[key], col) ) {
                    var dcol = elm('div').setClass('icol'+ cls).html(od[key][col]).appendTo(item);
                }
            }
        }

        if ( this.idata.childNodes.length===0 ) {
            this.nodata = elm('div').setClass('item odd').appendTo(this.idata);
            elm('div').setClass('icol none').html('&#27809;&#26377;&#36164;&#26009;').appendTo(this.nodata);
        } else {
            this.nodata && this.nodata.empty().remove() && delete this.nodata;
        }

    return this;
    },

    init : function () {
        this.$ = elm('div').appendTo(this.elem);
        this.id = this.$.id = p(this,'id') && !$(this.id) ? this.id : 'idata_'+ this.id + now(2)*Math.random();

        if ( this.tool ) {
            this.itool = elm('div').setClass('inav').appendTo(this.$);
            if ( p(this.tool, 'cls') ) {
                this.itool.addClass(this.tool.cls);
            }
            this.inav = (new inav({id:this.id +'_inav', elem:this.itool,menu:this.tool.menu})).init();
        }

        this.ititl = elm('div').setClass('idata').appendTo(this.$);
        this.idata = elm('form').setClass('idata').appendTo(this.$);

        this.seTitle().add(this.data);
    return this;
    }

};